package com.leo.quartz.controller.quartz;

import com.leo.base.AbstractController;
import com.leo.base.ReturnResponse;
import com.leo.quartz.common.lang.PageView;
import com.leo.quartz.common.lang.ReturnCodeConstant;
import com.leo.quartz.common.request.MyJobDetailRequestParam;
import com.leo.quartz.service.JobTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author liusl
 * @version 1.0
 * @date 2018/12/9 16:03
 */
@Controller
@RequestMapping("/quartz/detail")
public class QuartzTriggerController extends AbstractController {
    private JobTriggerService jobTriggerService;
    private ReturnResponse response;
    @RequestMapping("/detailList.html")
    public ModelAndView detailListView() {
        return this.buildModelAndView(PageView.Quartz.Detail.DETAIL_LIST);
    }

    @RequestMapping("/editDetail.html")
    public ModelAndView detailEditView(Long id) {
        try {
            response = jobTriggerService.getJobDtailById(id);
            return this.buildModelAndView(PageView.Quartz.JOB_EDIT, response);
        } catch (Exception ex) {
            return this.buildModelAndView(PageView.ERROR_404);
        }
    }

    @RequestMapping("/addJobDetail.html")
    public ModelAndView addView(Long id) {
        return this.buildModelAndView(PageView.Quartz.JOB_CREATE);
    }

    @RequestMapping("/triggerList")
    @ResponseBody
    public String queryJobDetailList(MyJobDetailRequestParam requestParam) {
        response = jobTriggerService.queryTriggerList(requestParam);
        return buildResult(response);
    }

    @RequestMapping("/removeJob")
    @ResponseBody
    private String removeJob(Long id) {
        try {
            response = jobTriggerService.removeJob(id);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }

    @RequestMapping("addJob")
    @ResponseBody
    private String addJob(MyJobDetailRequestParam param) {
        try {
            response = jobTriggerService.addJob(param);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }

    @RequestMapping("editJob")
    @ResponseBody
    private String editJob(MyJobDetailRequestParam param) {
        try {
            response = jobTriggerService.editJobDetail(param);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }

    public JobTriggerService getJobTriggerService() {
        return jobTriggerService;
    }
    @Autowired
    public void setJobTriggerService(JobTriggerService jobTriggerService) {
        this.jobTriggerService = jobTriggerService;
    }
}
